//
//  SLAddGoodsViewController.m
//  StarLight
//
//  Created by Duke.wu on 16/5/24.
//  Copyright © 2016年 Duke.Wu. All rights reserved.
//

#import "SLAddGoodsViewController.h"
#import "SLLabelFeild.h"
#import "SLLocatinViewController.h"
#import <QMapSearchKit/QMapSearchKit.h>
#import "SLDataPickViewController.h"
#import "STPopup.h"
#import "SLPackageOrBoatTypeViewController.h"
#import "SLNetWork.h"


@interface SLAddGoodsViewController ()

@property (nonatomic, strong) SLLabelFeild *goodsNameLF;

@property (nonatomic, strong) SLLabelFeild *startLF;

@property (nonatomic, strong) SLLabelFeild *finalLF;

@property (nonatomic, strong) SLLabelFeild *weightLF;

@property (nonatomic, strong) SLLabelFeild *pakgeLF;

@property (nonatomic, strong) SLLabelFeild *dateLF;

@property (nonatomic, strong) UIButton *dateBtn;

@property (nonatomic, strong) SLLabelFeild *noteLF;

@property (nonatomic, strong) UIButton *longTimeBtn;

@property (nonatomic, strong) UIButton *packageTypeBtn;

@property (nonatomic, strong) UIButton *confirmBtn;

@property (nonatomic, strong) UIView *contentView;

@property (nonatomic, strong) UIScrollView *mainScrollView;

@property (nonatomic, assign) BOOL isStartLoc;

@property (nonatomic, strong) NSMutableDictionary *jsonDic;

@end

@implementation SLAddGoodsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
  self.mainScrollView = [UIScrollView new];
  [self.view addSubview:self.mainScrollView];
  [self setUpUI];
  [self addConstrains];
  [self addObservers];
    // Do any additional setup after loading the view.
}

- (void)viewWillAppear:(BOOL)animated{
  [super viewWillAppear:animated];
  self.title = @"添加货源";
  [self.navigationController setNavigationBarHidden:NO animated:YES];
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:YES];
}

- (void)addObservers{
  @weakify(self)
  [[[NSNotificationCenter defaultCenter] rac_addObserverForName:@"changeLocation" object:nil] subscribeNext:^(NSNotification *notify) {
    @strongify(self)
    QMSPoiData *poi = notify.userInfo[@"location"];
    if (self.isStartLoc) {
      self.startLF.contentField.text = poi.title;
      [self.jsonDic setObject:poi.title forKey:@"qsgk"];
      [self.jsonDic setObject:@(poi.location.longitude) forKey:@"qsx"];
      [self.jsonDic setObject:@(poi.location.latitude) forKey:@"qsy"];
      
    }else{
      self.finalLF.contentField.text = poi.title;
      [self.jsonDic setObject:@(poi.location.longitude) forKey:@"ddx"];
      [self.jsonDic setObject:@(poi.location.latitude) forKey:@"ddy"];
      [self.jsonDic setObject:poi.title forKey:@"ddgk"];
    }
  }];
  
  [RACObserve(self.longTimeBtn, selected) subscribeNext:^(NSNumber *select) {
    @strongify(self);
    
    if (select.boolValue) {
      self.dateLF.contentField.text = @"长期有效";
      self.dateLF.contentField.enabled = NO;
    }else{
      self.dateLF.contentField.text = @"";
      self.dateLF.contentField.enabled = YES;
    }
    
  }];
  
  
}

#pragma mark - Private Methods

- (void)choseStart{
  self.isStartLoc = YES;
  [self toSkipLocationView];
}

- (void)choseFinal{
  self.isStartLoc = NO;
  [self toSkipLocationView];
}

- (void)toSkipLocationView{
  SLLocatinViewController *locationVC = [SLLocatinViewController new];
  [self.navigationController pushViewController:locationVC animated:YES];
}

- (void)endEdited{
  [self.contentView endEditing:YES];
}

- (void)selectLongTime{
  self.longTimeBtn.selected = !self.longTimeBtn.selected;
}

- (void)selectDate{
  SLDataPickViewController *datePickVC = [[SLDataPickViewController alloc] init];
  [self presentVCWithVC:datePickVC navigationBarHidden:YES];
  @weakify(self)
  [datePickVC returnDate:^(NSString *date) {
    @strongify(self)
    self.dateLF.contentField.text = date;
  }];

}

- (void)selectPackageType{
  SLPackageOrBoatTypeViewController *packageTypeVC = [[SLPackageOrBoatTypeViewController alloc] initWithType:PackageOrBoatTypePackage];
  [self presentVCWithVC:packageTypeVC navigationBarHidden:NO];
  
  @weakify(self)
  [packageTypeVC returnReturnPackageOrBoatType:^(NSString *type) {
    @strongify(self)
    self.pakgeLF.contentField.text = type;
  }];
}

- (void)presentVCWithVC:(UIViewController *)vc navigationBarHidden:(BOOL)hidden{
  STPopupController *popVC = [[STPopupController alloc] initWithRootViewController:vc];
  popVC.cornerRadius = 10;
  if (hidden) {
    popVC.navigationBarHidden = hidden;
  }
  [popVC presentInViewController:self];
}

- (void)confirmToAddGoods{
  
  NSString *goodsName = self.goodsNameLF.contentField.text;
  NSString *startLoc = self.startLF.contentField.text;
  NSString *finalLoc = self.finalLF.contentField.text;
  NSString *weight = self.weightLF.contentField.text;
  NSString *packageType = self.pakgeLF.contentField.text;
  NSString *date = self.dateLF.contentField.text;
  NSString *note = self.noteLF.contentField.text;
  
  
  
  if(weight.integerValue > 5000000){
    [SVProgressHUD showImage:nil status:@"最重5000000吨"];
    return;
  }
  
  
  if (goodsName.length > 0 && startLoc.length > 0 && finalLoc.length > 0 && weight.length > 0 && packageType.length > 0 && date.length > 0) {
    
    [self.jsonDic setObject:goodsName forKey:@"hwmc"];
    [self.jsonDic setObject:weight forKey:@"hwzw"];
    [self.jsonDic setObject:packageType forKey:@"bzxs"];
    [self.jsonDic setObject:date forKey:@"jzrq"];
    [self.jsonDic setObject:note forKey:@"xxbz"];
    [self.jsonDic setObject:[DEFAULTS objectForKey:@"loginInformation"][@"id"] forKey:@"hzid"];
    
    NSString *json = [self.jsonDic mj_JSONString];
    
    NSDictionary *parameter = @{@"datajson":json};
    NSString *postUrl = [NSString stringWithFormat:@"%@/goods/addgoods",baseUrl];
    @weakify(self)
    [SLNetWork postDataWithParameters:parameter postUrl:postUrl complete:^(id respond, NSError *error) {
      @strongify(self);
      if (!error) {
        [self.navigationController popViewControllerAnimated:YES];
      }
    }];
  }else{
    [SVProgressHUD showErrorWithStatus:@"请完善信息"];
  };

}



#pragma mark - setUp UI

- (void)setUpUI{
  self.view.backgroundColor = [UIColor defeutBackgroudColor];
  
  [self.mainScrollView addSubview:self.contentView];
  [self.contentView addSubview:self.goodsNameLF];
  [self.contentView addSubview:self.startLF];
  [self.contentView addSubview:self.finalLF];
  [self.contentView addSubview:self.weightLF];
  [self.contentView addSubview:self.pakgeLF];
  [self.contentView addSubview:self.dateLF];
  [self.contentView addSubview:self.noteLF];
  [self.contentView addSubview:self.longTimeBtn];
  [self.contentView addSubview:self.packageTypeBtn];
  [self.contentView addSubview:self.dateBtn];
  [self.view addSubview:self.confirmBtn];
  
  UITapGestureRecognizer *tapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(endEdited)];
  [self.view addGestureRecognizer:tapGes];
}

- (void)addConstrains{
  
  CGFloat linePading = 15;
  
  CGFloat lfPading = 20;
  
  [self.mainScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
    make.edges.equalTo(self.view);
  }];
  
  [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
    make.edges.equalTo(self.mainScrollView);
    make.width.equalTo(self.mainScrollView);
  }];
  
  [self.goodsNameLF mas_makeConstraints:^(MASConstraintMaker *make) {
    make.width.equalTo(self.contentView);
    make.centerX.top.equalTo(self.contentView);
    make.height.mas_equalTo(50*MIX_H);
  }];
  
  
//  UIImageView *firLine = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"line"]];
  UIView *firLine = [UIView xlineView];
  [self.view addSubview:firLine];
  [firLine mas_makeConstraints:^(MASConstraintMaker *make) {
    make.top.equalTo(self.goodsNameLF.mas_bottom);
    make.left.equalTo(self.goodsNameLF);
    make.right.equalTo(self.goodsNameLF);
    make.height.equalTo(@1);
  }];

  
  UIButton *startButton = [UIButton buttonWithType:UIButtonTypeCustom];
  [startButton setImage:[UIImage imageNamed:@"map2"] forState:UIControlStateNormal];
  [startButton addTarget:self action:@selector(choseStart) forControlEvents:UIControlEventTouchUpInside];
  [self.contentView addSubview:startButton];
  [startButton mas_makeConstraints:^(MASConstraintMaker *make) {
    make.top.equalTo(firLine.mas_bottom).offset(linePading);
    make.right.equalTo(self.goodsNameLF).offset(-20);
    make.size.mas_equalTo(CGSizeMake(40, 23));
  }];
  
  [self.startLF mas_makeConstraints:^(MASConstraintMaker *make) {
    make.centerY.equalTo(startButton);
    make.left.height.equalTo(self.goodsNameLF);
    make.right.equalTo(startButton.mas_left);
  }];
  
  UIButton *finalButton = [UIButton buttonWithType:UIButtonTypeCustom];
  [finalButton setImage:[UIImage imageNamed:@"map2"] forState:UIControlStateNormal];
  [finalButton addTarget:self action:@selector(choseFinal) forControlEvents:UIControlEventTouchUpInside];
  [self.contentView addSubview:finalButton];
  [finalButton mas_makeConstraints:^(MASConstraintMaker *make) {
    make.top.equalTo(self.startLF.mas_bottom).offset(lfPading);
    make.right.equalTo(self.goodsNameLF).offset(-20);
    make.size.mas_equalTo(CGSizeMake(40, 23));
  }];

  [self.finalLF mas_makeConstraints:^(MASConstraintMaker *make) {
    make.centerY.equalTo(finalButton);
    make.left.height.equalTo(self.goodsNameLF);
    make.right.equalTo(finalButton.mas_left);
  }];
  
//  UIImageView *secondLine = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"line"]];
  UIView *secondLine = [UIView xlineView];
  [self.contentView addSubview:secondLine];
  [secondLine mas_makeConstraints:^(MASConstraintMaker *make) {
    make.top.equalTo(self.finalLF.mas_bottom).offset(linePading);
    make.left.equalTo(self.goodsNameLF);
    make.right.equalTo(self.goodsNameLF);
    make.height.equalTo(@1);
  }];


  [self.weightLF mas_makeConstraints:^(MASConstraintMaker *make) {
    make.top.equalTo(secondLine.mas_bottom).offset(linePading);
    make.left.height.equalTo(self.finalLF);
    make.right.equalTo(finalButton.mas_left);
  }];
  
//  UIImageView *thirdLine = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"line"]];
  UIView *thirdLine = [UIView xlineView];
  [self.contentView addSubview:thirdLine];
  [thirdLine mas_makeConstraints:^(MASConstraintMaker *make) {
    make.top.equalTo(self.weightLF.mas_bottom).offset(linePading);
    make.left.equalTo(self.goodsNameLF);
    make.right.equalTo(self.goodsNameLF);
    make.height.equalTo(@1);
  }];
  
  [self.pakgeLF mas_makeConstraints:^(MASConstraintMaker *make) {
    make.top.equalTo(self.weightLF.mas_bottom).offset(lfPading);
    make.left.height.equalTo(self.finalLF);
    make.right.equalTo(finalButton.mas_left);
  }];
  
  [self.dateLF mas_makeConstraints:^(MASConstraintMaker *make) {
    make.top.equalTo(self.pakgeLF.mas_bottom);
    make.left.height.equalTo(self.finalLF);
    make.right.equalTo(finalButton.mas_left);
  }];
  
  
  [self.longTimeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
    make.centerY.equalTo(self.dateLF);
    make.right.equalTo(self.contentView);
    make.size.mas_equalTo(CGSizeMake(80, 20));
  }];
  
  [self.dateBtn mas_makeConstraints:^(MASConstraintMaker *make) {
    make.left.top.height.equalTo(self.dateLF.contentField);
    make.right.equalTo(self.longTimeBtn.mas_left);
  }];
  
  [self.packageTypeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
    make.left.top.height.equalTo(self.pakgeLF.contentField);
    make.right.equalTo(self.longTimeBtn.mas_left);
  }];
  
//  UIImageView *forthLine = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"line"]];
  UIView *forthLine = [UIView xlineView];
  [self.contentView addSubview:forthLine];
  [forthLine mas_makeConstraints:^(MASConstraintMaker *make) {
    make.top.equalTo(self.dateLF.mas_bottom);
    make.left.equalTo(self.goodsNameLF);
    make.right.equalTo(self.goodsNameLF);
    make.height.equalTo(@1);
  }];
  
  [self.noteLF mas_makeConstraints:^(MASConstraintMaker *make) {
    make.top.equalTo(forthLine.mas_bottom).offset(linePading);
    make.left.height.equalTo(self.finalLF);
    make.right.equalTo(self.goodsNameLF);
  }];

  [self.contentView mas_updateConstraints:^(MASConstraintMaker *make) {
    make.bottom.equalTo(self.noteLF);
  }];
  
  
  [self.confirmBtn mas_makeConstraints:^(MASConstraintMaker *make) {
    make.left.bottom.width.equalTo(self.view);
    make.height.mas_equalTo(45);
  }];
  
  
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - getter

- (UIView *)contentView{
  if (!_contentView) {
    _contentView = [UIView new];
    _contentView.backgroundColor = [UIColor whiteColor];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(endEdited)];
    [_contentView addGestureRecognizer:tap];
  }
  return _contentView;
}

- (SLLabelFeild *)noteLF{
  if (!_noteLF) {
    _noteLF = [[SLLabelFeild alloc] initWithTitle:@"其他备注:" textViewHolder:@"   "];
    _noteLF.limit = 200;
  }
  return _noteLF;
}

- (SLLabelFeild *)dateLF{
  if (!_dateLF) {
    _dateLF = [[SLLabelFeild alloc] initWithTitle:@"日期:" fieldPlaceHolder:@""];
  }
  return _dateLF;
}

- (SLLabelFeild *)pakgeLF{
  if (!_pakgeLF) {
    _pakgeLF = [[SLLabelFeild alloc] initWithTitle:@"包装形式:" fieldPlaceHolder:@"散装"];
  }
  return _pakgeLF;
}

- (SLLabelFeild *)weightLF{
  if (!_weightLF) {
    _weightLF = [[SLLabelFeild alloc] initWithTitle:@"重量:" fieldPlaceHolder:@"请输入重量"];
    _weightLF.contentField.keyboardType = UIKeyboardTypePhonePad;
  }
  return _weightLF;
}

- (SLLabelFeild *)finalLF{
  if (!_finalLF) {
    _finalLF = [[SLLabelFeild alloc] initWithTitle:@"目的码头:" fieldPlaceHolder:@"请选择目的码头"];
    _finalLF.contentField.enabled = NO;
    _finalLF.limit = 15;
  }
  return _finalLF;
}

- (SLLabelFeild *)startLF{
  if (!_startLF) {
    _startLF = [[SLLabelFeild alloc] initWithTitle:@"装货码头:" fieldPlaceHolder:@"请选择装货码头"];
    _startLF.contentField.enabled = NO;
    _startLF.limit = 15;
  }
  return _startLF;
}


- (SLLabelFeild *)goodsNameLF{
  if (!_goodsNameLF) {
    _goodsNameLF = [[SLLabelFeild alloc] initWithTitle:@"货源名称:" fieldPlaceHolder:@"请输入货源名称"];
    _goodsNameLF.limit = 8;
  }
  return _goodsNameLF;
}
- (UIButton *)longTimeBtn{
  if (!_longTimeBtn) {
    _longTimeBtn = [UIButton buttonWithTitle:@"长期有效"];
    _longTimeBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    [_longTimeBtn setImage:[UIImage imageNamed:@"Not checked"] forState:UIControlStateNormal];
    [_longTimeBtn setImage:[UIImage imageNamed:@"Check"] forState:UIControlStateSelected];
    [_longTimeBtn addTarget:self action:@selector(selectLongTime) forControlEvents:UIControlEventTouchUpInside];
    [_longTimeBtn dw_imageTitleHorizontalAlignmentWithSpace:4];
  }
  return _longTimeBtn;
}


- (UIButton *)confirmBtn{
  if (!_confirmBtn) {
    _confirmBtn = [UIButton buttonWithTitle:@"保存"];
    [_confirmBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    _confirmBtn.backgroundColor = SLYellowColor;
    [_confirmBtn addTarget:self action:@selector(confirmToAddGoods) forControlEvents:UIControlEventTouchUpInside];
  }
  return _confirmBtn;
}

- (UIButton *)dateBtn{
  if (!_dateBtn) {
    _dateBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [_dateBtn addTarget:self action:@selector(selectDate) forControlEvents:UIControlEventTouchUpInside];
  }
  return _dateBtn;
}

- (UIButton *)packageTypeBtn{
  if (!_packageTypeBtn) {
    _packageTypeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [_packageTypeBtn addTarget:self action:@selector(selectPackageType) forControlEvents:UIControlEventTouchUpInside];
  }
  return _packageTypeBtn;
}

- (NSMutableDictionary *)jsonDic{
  if (!_jsonDic) {
    _jsonDic = [[NSMutableDictionary alloc] init];
  }
  return _jsonDic;
}


- (UIScrollView *)mainScrollView{
  if (!_mainScrollView) {
    _mainScrollView = [UIScrollView new];
  }
  return _mainScrollView;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
